﻿using System;
using System.Data.Services;
using System.Data.Services.Common;
using System.ServiceModel;

using TheKitchen.Querying.Core.DataAccess;
using System.Linq;
using System.ServiceModel.Web;

namespace TheKitchen.Querying
{
    /// <summary>
    /// Exposes our Entity Framework data model as a REST-based service.
    /// </summary>
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class QueryService : DataService<QueryContext>
    {
        /// <summary>
        /// Initializes the service.
        /// </summary>
        /// <param name="config">The config.</param>
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.UseVerboseErrors = true;
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("FindRecipes", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebGet]
        public IQueryable<Recipe> FindRecipes(string partialName, string partialDescription, int minimumRating)
        {
            return from recipe in CurrentDataSource.Recipes
                   where (partialName.Length == 0 || (recipe.Name.Contains(partialName)))
                   where (partialDescription.Length == 0 || (recipe.Description.Contains(partialDescription)))
                   where (recipe.Rating > minimumRating)
                   select recipe;
        }
    }
}
